export default{
	// 计算当前日期星座
	getHoroscope(date) {
	  let c = ['摩羯','水瓶','双鱼','白羊','金牛','双子','巨蟹','狮子','处女','天秤','天蝎','射手','摩羯']
	  date=new Date(date);
	  let month = date.getMonth() + 1;
	  let day = date.getDate();
	  let startMonth = month - (day - 14 < '865778999988'.charAt(month));
	  return c[startMonth]+'座';
	},
	// 计算指定时间与当前的时间差
	sumAge(data){
		let dateBegin = new Date(data.replace(/-/g, "/"));
		let dateEnd = new Date();//获取当前时间
		let dateDiff = dateEnd.getTime() - dateBegin.getTime();//时间差的毫秒数
		let dayDiff = Math.floor(dateDiff / (24 * 3600 * 1000));//计算出相差天数
		let leave1=dateDiff%(24*3600*1000)    //计算天数后剩余的毫秒数
		let hours=Math.floor(leave1/(3600*1000))//计算出小时数
		//计算相差分钟数
		let leave2=leave1%(3600*1000)    //计算小时数后剩余的毫秒数
		let minutes=Math.floor(leave2/(60*1000))//计算相差分钟数
		//计算相差秒数
		let leave3=leave2%(60*1000)      //计算分钟数后剩余的毫秒数
		let seconds=Math.round(leave3/1000)
		return dayDiff+"天 "+hours+"小时 ";
	},
	// 获取聊天时间（相差300s内的信息不会显示时间）
	getChatTime(v1,v2){
		v1=v1.toString().length<13 ? v1*1000 : v1;
		v2=v2.toString().length<13 ? v2*1000 : v2;
		if(((parseInt(v1)-parseInt(v2))/1000) > 300){
			return this.gettime(v1);
		}
	},
	// 人性化时间格式
	gettime(shorttime){
		
		if(shorttime){
			shorttime=shorttime.toString().length<13 ? shorttime*1000 : shorttime;
			let now = (new Date()).getTime();
			let cha = (now-parseInt(shorttime))/1000;
			if (cha < 43200) {
				// 当天
				return this.dateFormat(new Date(shorttime),"{A} {t}:{ii}");
			} else if(cha < 518400){
				// 隔天 显示日期+时间
				return this.dateFormat(new Date(shorttime),"{Mon}月{DD}日 {A} {t}:{ii}");
			} else {
				// 隔年 显示完整日期+时间
				return this.dateFormat(new Date(shorttime),"{Y}-{MM}-{DD} {A} {t}:{ii}");
			}
		}
		return ''
	},
	
	parseNumber(num) {
		return num < 10 ? "0" + num : num;
	},
	/**
	 * @param {Object} val 
	 * @param {Object} fmt
	 * pattern("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423       
	 * pattern("yyyy-MM-dd E HH:mm:ss") ==> 2009-03-10 二 20:09:04       
	 * pattern("yyyy-MM-dd EE hh:mm:ss") ==> 2009-03-10 周二 08:09:04       
	 * pattern("yyyy-MM-dd EEE hh:mm:ss") ==> 2009-03-10 星期二 08:09:04       
	 * pattern("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18  
	 */
	pattern(data,fmt){
		let time = data.toString().length<13 ?data*1000:data
		let date=new Date(time);
		let o = {           
		     "M+" : date.getMonth()+1, //月份           
		     "d+" : date.getDate(), //日           
		     "h+" : date.getHours()%12 == 0 ? 12 : date.getHours()%12, //小时           
		     "H+" : date.getHours(), //小时           
		     "m+" : date.getMinutes(), //分           
		     "s+" : date.getSeconds(), //秒                 
		     "S" : date.getMilliseconds() //毫秒           
		}
		let week = {           
		    "0" : "/u65e5",           
		    "1" : "/u4e00",           
		    "2" : "/u4e8c",           
		    "3" : "/u4e09",           
		    "4" : "/u56db",           
		    "5" : "/u4e94",           
		    "6" : "/u516d"          
		    }
		if(/(y+)/.test(fmt)){           
			fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));           
		}           
		if(/(E+)/.test(fmt)){           
			fmt=fmt.replace(RegExp.$1, ((RegExp.$1.length>1) ? (RegExp.$1.length>2 ? "/u661f/u671f" : "/u5468") : "")+week[date.getDay()+""]);           
		}
		for(let k in o){
			if(new RegExp("("+ k +")").test(fmt)){           
				fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));           
			}
		}
		return fmt; 
	},
	diffHour(d1,d2){
		var timeDiff = d2.getTime() - d1.getTime();
		var hours = Math.ceil(timeDiff / (1000 * 3600 ));
		return hours
	},
	format(date, formatStr) {
		let dateObj = {},
			rStr = /\{([^}]+)\}/,
			mons = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'];
		 
		dateObj["Y"] = date.getFullYear();
		dateObj["M"] = date.getMonth() + 1;
		dateObj["MM"] = this.parseNumber(dateObj["M"]);
		dateObj["Mon"] = mons[dateObj['M'] - 1];
		dateObj["D"] = date.getDate();
		dateObj["DD"] = this.parseNumber(dateObj["D"]);
		dateObj["h"] = date.getHours();
		dateObj["hh"] = this.parseNumber(dateObj["h"]);
		dateObj["t"] = dateObj["h"] > 12 ? dateObj["h"] - 12 : dateObj["h"];
		dateObj["tt"] = this.parseNumber(dateObj["t"]);
		dateObj["A"] = dateObj["h"] > 12 ? '下午' : '上午';
		dateObj["i"] = date.getMinutes();
		dateObj["ii"] = this.parseNumber(dateObj["i"]);
		dateObj["s"] = date.getSeconds();
		dateObj["ss"] = this.parseNumber(dateObj["s"]);
	 
		while(rStr.test(formatStr)) {
			formatStr = formatStr.replace(rStr, dateObj[RegExp.$1]);
		}
		return formatStr;
	},
	// 获取年龄
	getAgeByBirthday(data){
		let birthday=new Date(data.replace(/-/g, "\/")); 
		let d=new Date(); 
		return d.getFullYear()-birthday.getFullYear()-((d.getMonth()<birthday.getMonth()|| d.getMonth()==birthday.getMonth() && d.getDate()<birthday.getDate())?1:0);
	},
	offsetHour(date,n){
	 	let t = 60*60*1000
	 	let times = new Date(date).getTime()+ n*t
	 	return new Date(times) // new Date(date).getTime()	+ n*t
	},
	offsetDay(date,n){
		let t = 24*60*60*1000
		
		let times = new Date(date).getTime()+ n*t
		return new Date(times) // new Date(date).getTime()	+ n*t
	},
	beginOfDay(date){
		return new Date(date.toDateString()).getTime()
	},
	endOfDay(date){
		let a = new Date(date.toLocaleDateString()).getTime()
		return a+24*60*60*1000-1
	},
	beginOfWeek(date){
		let day = date.getDay()
		let t = 24*60*60*1000
		// 星期一时间戳
		let monday = date.getTime()-(day-1)*t
		// 周一0时
		return  new Date(new Date(monday).toDateString()).getTime() 
	},
	endOfWeek(date){
		let day = date.getDay()
		let t = 24*60*60*1000
		// 周日时间戳
		let sunday = date.getTime()+(7-day)*t
		// 周日23时
		let a =  new Date(new Date(sunday).toDateString()).getTime() 
		return a+24*60*60*1000-1
	},
	beginOfMonth(date){
		date.setDate(1)
		// 周一0时
		return  new Date(new Date(date.getTime()).toDateString()).getTime() 
	},
	endOfMonth(date){
		date.setMonth(date.getMonth() + 1)
		date.setDate(0)
		// 周一0时
		let a =  new Date(date.toDateString()).getTime()
		return  new Date(a+24*60*60*1000-1) 
	},
	dateName(data){
		// 返回 今天 昨天  明天 以及 n天前 n天后
		const date = 24 * 60 * 60 * 1000
		const thatDate = new Date(data)
		const now = new Date()
		// 设置 0时 0分 0秒 0毫秒
		thatDate.setHours(0, 0, 0, 0)
		now.setHours(0, 0, 0, 0)
		const n = thatDate.getTime() - now.getTime()
		if (n == 0) {
		  return '今天'
		} else if (n > 0) {
		  const days = n / date
		  if (days == 1) {
		    return '明天'
		  } else {
		    return days + '天后'
		  }
		} else if (n < 0) {
		  const days = Math.abs(n) / date
		  if (days == 1) {
		    return '昨天'
		  } else {
		    return days + '天前'
		  }
		}
	}
	
}